require.config({
    paths: {
        'axios': 'axios-0.19.0.min'
    }
})

require(['core', 'template-web', 'layer', 'printer', 'axios', 'drawer'], function (c, tpl, layer, printer, axios, drawer) {
    axios.get('/api/label/default-format.json').then(function (resp) {
        var html = tpl('data-input-tpl', {
            items: resp.data.items
        })
        c.one('.data-input').innerHTML = html
    }).catch(function (err) {
        layer.closeAll()
        layer.open({ content: err || '加载默认标签失败！', btn: '好的' })
    })

    // init drawer
    drawer.init('.actions a', 'design')

    function addPrintPage(init, items, pageNo, code) {
        if (init) printer.init()
        printer.setPrintPageSize(0, 2800, 760)
        if (!items.length) {
            layer.open({ content: '请先在设计区域(虚线框内)添加要打印的内容！', btn: '好的' })
            return
        }
        printer.setPrintStyle('FontSize', 11)
        pageNo = pageNo || 0
        var offset = (pageNo * (1 + items.length)) + 1
        c.each(items, function (item, index) {
            var x = parseInt(item.style.left)
            var y = parseInt(item.style.top)
            var width = parseInt(item.style.width) || 200
            var height = parseInt(item.style.height) || 24
            var type = c.data(item, 'type')
            switch (type) {
                case 'text':
                case 'code':
                    var value = type == 'code' ? code || item.value : item.value
                    printer.addPrintText(x, y, width, height, value || '未设置打印内容')
                    var fontName = item.style.fontFamily
                    var fontSize = parseInt(item.style.fontSize) || 11
                    // printer.setPrintFontName(index + offset, fontName)
                    // printer.setPrintFontSize(index + offset, fontSize)
                    break
                case 'barcode':
                    break;
                case 'image':
                case 'ccc':
                case 'qrcode':
                    printer.addPrintImage(x, y, width, height, item.src)
                    break
            }
        })
        printer.addPrintRect(0, 0, 640, 480, 0, 0)
    }

    function formatCode(code, length) {
        length = length || 1
        code += ''
        return code.length >= length ? code : '0'.repeat(length - code.length) + code
    }

    var actions = {
        preview: function () {
            var from = c.one('.btns input[name="from"]').value
            var items = c.all('form .label-item')
            var template = c.one('form [data-type="code"]').value
            var numPart = template.match(/\d+/)[0]
            var nonNumPart = template.replace(/[\d]+/, '')
            var code = nonNumPart + formatCode(parseInt(from), numPart.length)
            var params = 'code=' + code
            c.each(items, function (item) {
                var name = item.name
                var value = item.value
                if (name && value) {
                    params += ('&' + name + '=' + value)
                }
            })
            layer.open({
                type: 1,
                content: '<img src="/api/print-preview?' + params + '">',
                anim: 'up',
                style: 'border-radius: 4px;',
                btn: ['关闭'],
                yes: function (index) {
                    layer.close(index);
                }
            })
        },
        print: function () {
            var from = c.one('.btns input[name="from"]').value
            var to = c.one('.btns input[name="to"]').value
            var count = (to - from) + 1
            /*if (count > 3) {
                count = 3
                layer.open({ content: '测试版本最多打3张！', btn: '好的' })
            }*/
            var items = c.all('form .label-item')
            var params = { f: from, c: String(count) }
            c.each(items, function (item) {
                var name = item.name
                var value = item.value
                if (name && value) {
                    params[name] = value
                }
            })
            axios.post('/api/print', params).then(function (resp) {
                layer.open({ content: '成功发送打印请求！', btn: '好的' })
            }).catch(function (err) {
                layer.closeAll()
                layer.open({ content: err || '打印失败！', btn: '好的' })
            })
        }
    }

    c.each(c.all('.btns button'), function (btn) {
        btn.addEventListener('click', function () {
            var fn = actions[c.data(btn, 'action')]
            if (fn) fn(btn)
        })
    })
})
